<?php
if($_POST['siteid']){
	$siteid = $_POST['siteid'];
	//echo $siteid;
}	

?>
<?php
//generate monthly calendar from today's date
$date = time();

if($_POST['d']){
	$day = $_POST['d'];
}else{
	$day = date('d',$date);
}
if($_POST['m']){
	$month = $_POST['m'];
}else{
	$month = date('m',$date);
}
if($_POST['Y']){
	$year = $_POST['Y'];
}else{
	$year = date('Y',$date);
}
$prevmonth = $month;
$nextmonth = $month;
if($month == 1){
	$prevmonth = 12;
	$nextmonth = 2;
	$oldyear = $year - 1;
}else{
	$oldyear = $year;
}
if($month == 12){
	$prevmonth = 11;
	$nextmonth = 1;
	$newyear = $year + 1;
}else{
	$newyear = $year;
}
if($month !=12 && $month !=1){
	$prevmonth--;
	$nextmonth++;
}
/* events overlay from db 
$siteid = 1; //$_POST['siteid'];
 //dbSelect('posts',array("site_id=$siteid","type_id=2","month(startdate)=$month","year(startdate)=$year"));
$eventsqry = mysql_query("SELECT * FROM posts WHERE site_id=".$siteid." and type_id=2 and month(startdate)=".$month." and year(startdate)=".$year." order by startdate DESC");
//
if(mysql_numrows($eventsqry) > 0){
	while($eventrow = mysql_fetch_array($eventsqry)){
		$thisday = date('j',dateFormat($eventrow['startdate']));
		$events[$thisday] = $thisday;
		echo $thisday;
	}
}else{
	//echo 'n/e';
}
*/
$first_day = mktime(0,0,0,$month,1,$year);
$title = date('F',$first_day);
$day_of_week =  date('D',$first_day);

switch($day_of_week){
	case "Sun":
		$blank = 0; 
		break;
	case "Mon":
		$blank = 1; 
		break;
	case "Tue":
		$blank = 2; 
		break;
	case "Wed":
		$blank = 3; 
		break;
	case "Thu":
		$blank = 4; 
		break;
	case "Fri":
		$blank = 5; 
		break;
	case "Sat":
		$blank = 6; 
		break;
}

$days_in_month = cal_days_in_month(0,$month,$year);
$tablewidth = 240;
$columnwidth = round($tablewidth/7);
echo '
<table align="center">
	<tr>
		<td><span class="fakelink" onclick="ajaxCalendar('.$prevmonth.','.$oldyear.');">prev</span></td>
		<td align="center" colspan="7" width="150px" style="font-weight: bold;">'.$title.' '.$year.'</td>
		<td><span class="fakelink" onclick="ajaxCalendar('.$nextmonth.','.$newyear.');">next</span></td>
	</tr>
</table>';
echo '<table width="100%" align="center" cellspacing="2" cellpadding="0"><tr class="headerrow"><td>Su</td><td>M</td><td>Tu</td><td>W</td><td>Th</td><td>F</td><td>Sa</td></tr>';

$day_count = 1;
echo '<tr>';

//beginning blank days
while($blank > 0){
	echo '<td width="'.$columnwidth.'"></td>';
	$blank = $blank - 1;
	$day_count++;
}

$day_num = 1;
//numbered days
while($day_num <= $days_in_month){ 
if($events[$day_num]){
	echo '<td id="day'.$day_num.'" width="'.$columnwidth.'" align="center">
	<span class="daywithevents" onclick="sendEvent('.$year.','.$month.','.$day_num.');">'.$day_num.'</span>
	</td>';
}else{
	echo '<td title="" id="day'.$day_num.'" width="'.$columnwidth.'" align="center">'.$day_num.'</td>';
}
	$day_num++;
	$day_count++;
	
	if($day_count > 7){
		echo '</tr><tr>';
		$day_count = 1;
	}
}

//ending blank days
while($day_count > 1 && $day_count <= 7){
	echo '<td width="'.$columnwidth.'"></td>';
	$day_count++;
}

echo '</tr></table>';
?>
<form id="calendarform" action="<?php echo $thisroot; ?>/events.php" method="post">
	<input type="hidden" id="ybox" name="y" />
	<input type="hidden" id="mbox" name="m" />
	<input type="hidden" id="dbox" name="d" />
</form>
<?php
 /*
 <table width="600">
 <tr>
 <td colspan="60">November 2009</td>
 </tr>
 <tr><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>
 <tr>
	 <td align="center">1</td>
	 <td align="center">2</td>
	 <td align="center">3</td>
	 <td align="center">4</td>
	 <td align="center">5</td>
	 <td align="center">6</td>
	 <td align="center">7</td>
	 <td align="center">8</td>
 </tr>
 <tr><td align="center">9</td><td align="center">10</td><td align="center">11</td><td align="center">12</td><td align="center">13</td><td align="center">14</td><td align="center">15</td></tr><tr><td align="center">16</td><td align="center">17</td><td align="center">18</td><td align="center">19</td><td align="center">20</td><td align="center">21</td><td align="center">22</td></tr><tr><td align="center">23</td><td align="center">24</td><td align="center">25</td><td align="center">26</td><td align="center">27</td><td align="center">28</td><td align="center">29</td></tr><tr><td align="center">30</td><td></td><td></td><td></td><td></td><td></td><td></td></tr></table>				</div>

			
 */
?>